package com.totoro.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.totoro.domain.MySysRemarkTemplate;
import com.totoro.domain.Tutor;
import com.totoro.service.MySysRemarkTemplateService;
import com.totoro.service.StaffService;
import com.totoro.util.CommonConstant;
import com.totoro.util.Page;

@Controller
@RequestMapping("/mySysRemarkTemplate")
public class MySysRemarkTemplateController {

	@Autowired
	private MySysRemarkTemplateService mySysRemarkTemplateService;
	@Autowired
	private StaffService staffService;
	
	public ModelAndView list(Authentication authentication) {
		//获取用户名
		String username = authentication.getName();
		System.out.println("用户名：" + username);
		Tutor tutor = staffService.uniqueResult("no", username);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/tutor/tutorialList");
		mav.addObject("tutorialList", tutor.getTutorial());
		return mav;
	}
	
	@RequestMapping("/list")
	public ModelAndView list(Integer pageNo,Authentication authentication) {
		String username = authentication.getName();
		Tutor tutor = staffService.uniqueResult("no", username);
		System.out.println(tutor.getName());
		System.out.println("/mySysRemarkTemplate/list");
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/mySysRemarkTemplate/list");						  
		
		pageNo = pageNo == null ? 1 : pageNo;
		Page<MySysRemarkTemplate> pagedPost = mySysRemarkTemplateService.getPagePosts(pageNo,
				CommonConstant.PAGE_SIZE);
		
		mav.addObject("pagedPost", pagedPost);
		mav.addObject("mySysRemarkTemplateList", pagedPost.getResult());
		mav.addObject("pageNo", pageNo);
		return mav;
	}

	@RequestMapping("/findByName")
	public ModelAndView findByName(String keyword) {
		System.out.println("description: " + keyword);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/mySysRemarkTemplate/list");
		mav.addObject("sysRemarkTemplateList",
				mySysRemarkTemplateService.list("keyword", new String(keyword)));
		return mav;
	}

	@RequestMapping(value = "/saveOrUpdate", method = RequestMethod.GET)
	public String toSave(ModelMap modelMap) {	
		modelMap.addAttribute("mySysRemarkTemplate",new MySysRemarkTemplate());
		return "/mySysRemarkTemplate/saveOrUpdate";
	}
//	保存或更新
	@RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
	public String saveOrUpdate(@ModelAttribute("mySysRemarkTemplate") MySysRemarkTemplate mySysRemarkTemplate) {
		System.out.println("mySysRemarkTemplate: " + mySysRemarkTemplate);
//		如果id为null，则为save，否则update
		mySysRemarkTemplateService.saveOrUpdate(mySysRemarkTemplate);
		return "redirect:/mySysRemarkTemplate/list.html";
	}

	@RequestMapping("/findById/{id}")
	public String findById(@PathVariable("id") Integer id, ModelMap modelMap) {
		modelMap.addAttribute("mySysRemarkTemplate", mySysRemarkTemplateService.get(id));
		return "/mySysRemarkTemplate/saveOrUpdate";
	}

//	@RequestMapping("/update")
//	public String update(@ModelAttribute("mySysRemarkTemplate") MySysRemarkTemplate mySysRemarkTemplate) {
//		System.out.println(mySysRemarkTemplate);
//		mySysRemarkTemplate.update(mySysRemarkTemplate);
//		return "redirect:/mySysRemarkTemplate/list.html";
//	}

	@RequestMapping("/delete/{id}")
	public String delete(MySysRemarkTemplate mySysRemarkTemplate) {
		mySysRemarkTemplateService.deleteObject(mySysRemarkTemplate);
		return "redirect:/mySysRemarkTemplate/list.html";
	}

	//增加评语模板json视图
    @RequestMapping("/findById")
    public Object findByIdToJson(Integer id, ModelMap model){
        MySysRemarkTemplate mySysRemarkTemplate = mySysRemarkTemplateService.get(id);
        model.addAttribute("description", mySysRemarkTemplate.getDescription());
        return "mySysRemarkTemplateJson";
    }
	
}
